package com.neusoft.service.impl;

import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.neusoft.dao.PaperDao;
import com.neusoft.dao.impl.PaperDaoImpl;
import com.neusoft.domain.Department;
import com.neusoft.domain.Job;
import com.neusoft.domain.paper.HireMapper;
import com.neusoft.domain.paper.HirePaperFilter;
import com.neusoft.domain.paper.New_employee_paper;
import com.neusoft.domain.paper.leave_employee_paper;
import com.neusoft.domain.paper.tf_employee;
import com.neusoft.domain.paper.dptTf_employee_paper;
import com.neusoft.domain.paper.jbTf_employee_paper;
import com.neusoft.domain.paper.leaveMapper;
import com.neusoft.service.PaperService;

@Service("paperServiceImpl")
public class PaperServiceImpl implements PaperService{

	@Resource(name="paperDaoImpl")
	private PaperDao paperDao; 
	

	public void setPaperDao(PaperDao paperDao) {
		this.paperDao = paperDao;
	}

	@Override
	public List<HireMapper> getUsers(HirePaperFilter hirePaperFilter) {
		int department;
		if(hirePaperFilter.getUser_department()!=""){
			department = Integer.parseInt(hirePaperFilter.getUser_department());
			String user_department = getUser_department(department);
			hirePaperFilter.setUser_department(user_department);
			System.out.println(department+"---------"+hirePaperFilter.getUser_department()+"---------");
			List<New_employee_paper> nwe = this.paperDao.getUsers(hirePaperFilter);
			Iterator<New_employee_paper> n = nwe.iterator();
			New_employee_paper nwep = null;
			List<HireMapper> new_employees = new ArrayList<HireMapper>();
			while(n.hasNext()){
				HireMapper mapper = new HireMapper();
				nwep = n.next();
				mapper.setUser_id(nwep.getUser_id());
				mapper.setUser_department(nwep.getUser_department());
				mapper.setUser_job(nwep.getUser_job());
				mapper.setUser_name(nwep.getUser_name());
				mapper.setUser_sex(nwep.getUser_sex());
				mapper.setUser_work_date(DateFormat.getDateInstance().format(nwep.getUser_work_date()));
				mapper.setUser_degree(nwep.getUser_degree());
				new_employees.add(mapper);
			}
			return new_employees;
		}
		else{
			List<New_employee_paper> nwe = this.paperDao.getUsersAll(hirePaperFilter);
			Iterator<New_employee_paper> n = nwe.iterator();
			New_employee_paper nwep = null;
			List<HireMapper> new_employees = new ArrayList<HireMapper>();
			while(n.hasNext()){
				HireMapper mapper = new HireMapper();
				nwep = n.next();
				mapper.setUser_id(nwep.getUser_id());
				mapper.setUser_department(nwep.getUser_department());
				mapper.setUser_job(nwep.getUser_job());
				mapper.setUser_name(nwep.getUser_name());
				mapper.setUser_sex(nwep.getUser_sex());
				mapper.setUser_work_date(DateFormat.getDateInstance().format(nwep.getUser_work_date()));
				mapper.setUser_degree(nwep.getUser_degree());
				new_employees.add(mapper);
			}
			return new_employees;
		}
	}

	@Override
	public String getUser_department(int department) {
		return this.paperDao.getUser_department(department);
	}

	@Override
	public List<Department> getDepartment() {
		return this.paperDao.getDepartment();
	}

	@Override
	public List<leaveMapper> getFiredUsers(HirePaperFilter hirePaperFilter) {
		int department;
		if(hirePaperFilter.getUser_department()!=""){
			department = Integer.parseInt(hirePaperFilter.getUser_department());
			String user_department = getUser_department(department);
			hirePaperFilter.setUser_department(user_department);
			List<leave_employee_paper> leave_em = this.paperDao.getFiredUsers(hirePaperFilter);
			Iterator<leave_employee_paper> leave = leave_em.iterator();
			leave_employee_paper lea = null;
			List<leaveMapper> leave_employees = new ArrayList<leaveMapper>();
			while(leave.hasNext()){
				leaveMapper mapper = new leaveMapper();
				lea = leave.next();
				mapper.setUser_id(lea.getUser_id());
				mapper.setUser_department(lea.getUser_department());
				mapper.setUser_job(lea.getUser_job());
				mapper.setUser_name(lea.getUser_name());
				mapper.setUser_sex(lea.getUser_sex());
				mapper.setOutjob_date(DateFormat.getDateInstance().format(lea.getOutjob_date()));
				mapper.setOutjob_type(lea.getOutjob_type());
				leave_employees.add(mapper);
			}
			return leave_employees;
		}
		else{
			List<leave_employee_paper> leave_em = this.paperDao.getFiredUsersAll(hirePaperFilter);
			Iterator<leave_employee_paper> leave = leave_em.iterator();
			leave_employee_paper lea = null;
			List<leaveMapper> leave_employees = new ArrayList<leaveMapper>();
			while(leave.hasNext()){
				leaveMapper mapper = new leaveMapper();
				lea = leave.next();
				mapper.setUser_id(lea.getUser_id());
				mapper.setUser_department(lea.getUser_department());
				mapper.setUser_job(lea.getUser_job());
				mapper.setUser_name(lea.getUser_name());
				mapper.setUser_sex(lea.getUser_sex());
				mapper.setOutjob_date(DateFormat.getDateInstance().format(lea.getOutjob_date()));
				mapper.setOutjob_type(lea.getOutjob_type());
				leave_employees.add(mapper);
			}
			return leave_employees;
		}
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.neusoft.service.PaperService#getDptTfUsers(com.neusoft.domain.paper.HirePaperFilter)
	 * 部门调转
	 */

	@Override
	public List<tf_employee> getDptTfUsers(HirePaperFilter hirePaperFilter) {
		List<dptTf_employee_paper> dptTf_employee_papers = this.paperDao.getDptTfUsers(hirePaperFilter);
		Iterator <dptTf_employee_paper> ds = dptTf_employee_papers.iterator();
		dptTf_employee_paper dp = null;
		List<tf_employee> tf_employees = new ArrayList<tf_employee>();
		while (ds.hasNext()) {
			//若在循环外只会往List添一个值
			tf_employee tf_user = new tf_employee();
			dp = ds.next();
			tf_user.setUser_id(dp.getUser_id());
			//将id转为name
			tf_user.setDpt_original(getUser_department(dp.getDpt_original()));
			tf_user.setDpt_transfer(getUser_department(dp.getDpt_transfer()));
			tf_user.setUser_name(dp.getUser_name());
			tf_user.setUser_sex(dp.getUser_sex());
			tf_user.setTf_date(DateFormat.getDateInstance().format(dp.getTf_date()));
			tf_user.setTf_reason(dp.getTf_reason());
			tf_employees.add(tf_user);
		}
		return tf_employees;
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.neusoft.service.PaperService#getJbTfUsers(com.neusoft.domain.paper.HirePaperFilter)
	 * 岗位调转
	 */

	@Override
	public List<tf_employee> getJbTfUsers(HirePaperFilter hirePaperFilter) {
		List<jbTf_employee_paper> jbTf_employee_papers = this.paperDao.getJbTfUsers(hirePaperFilter);
		Iterator<jbTf_employee_paper> ds = jbTf_employee_papers.iterator();
		jbTf_employee_paper jb = null;
		List<tf_employee> tf_employees = new ArrayList<tf_employee>();
		while (ds.hasNext()) {
			//若在循环外只会往List添一个值
			tf_employee tf_user = new tf_employee();
			jb = ds.next();
			tf_user.setUser_id(jb.getUser_id());
			tf_user.setUser_department(jb.getUser_department());
			//将id转为name
			tf_user.setJob_original(getUser_job(jb.getJob_original()));
			tf_user.setJob_transfer(getUser_job(jb.getJob_transfer()));
			tf_user.setUser_name(jb.getUser_name());
			tf_user.setUser_sex(jb.getUser_sex());
			tf_user.setTf_date(DateFormat.getDateInstance().format(jb.getTf_date()));
			tf_user.setTf_reason(jb.getTf_reason());
			tf_employees.add(tf_user);
		}
		return tf_employees;
	}

	@Override
	public String getUser_job(int job) {
		
		return this.paperDao.getUser_job(job);
	}
}
